package org.sgx.gwteditors.testgallery.gallery;

import java.util.Map;

import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.simple.BooleanEditor;
import org.sgx.gwteditors.client.util.GUIUtil;
import org.sgx.gwteditors.client.util.TestUtil;
import org.sgx.gwteditors.testgallery.gallery.app.EditorMetainfo;
import org.sgx.gwteditors.testgallery.gallery.app.EditorMetainfo.MetaInfo;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ExternalTextResource;
import com.google.gwt.resources.client.ResourceCallback;
import com.google.gwt.resources.client.ResourceException;
import com.google.gwt.resources.client.TextResource;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SimplePanel;

/**
 * @author sg
 * 
 */
public class EditorsGalleryTest implements Test {

	private EditorMetainfo metaInfo;
	private Grid grid;

	@Override
	public void test(Panel parent) {
		new EditorFramework1().start();
		metaInfo = new EditorMetainfo();

		parent.add(new HTML("<h1><a href=\"http://code.google.com/p/gwteditors/\">GwtEditors</a> - All editors list</h1>"));
		parent.add(new HTML("<p><i>by Sebastián Gurin</i></p>")); 
		grid = new Grid(metaInfo.getInfos().size() + 1, 5);
		parent.add(grid);
		grid.setSize("100%", "100%");

		printHeaders();

		int i = 1;
		for (MetaInfo info : metaInfo.getInfos()) {
			printInfo(parent, info, i);
			i++;
		}

	}

	private void printHeaders() {

		grid.setWidget(0, 0, new HTML("<h3>Editor's name</h3>"));

		grid.setWidget(0, 1, new HTML("<h3>Description</h3>"));

		grid.setWidget(0, 2, new HTML("<h3>Example</h3>"));

		grid.setWidget(0, 3, new HTML("<h3>Source</h3>"));

		grid.setWidget(0, 4, new HTML("<h3>Is Simple?</h3>"));

	}

	private void printInfo(Panel parent, final MetaInfo info, int i) {
		grid.setWidget(i, 0, new HTML("<h3>" + info.getName() + "</h3>"));

		grid.setWidget(i, 1, new HTML("<p><i>" + info.getDescription() + "</i></p>"));

		SimplePanel sp = new SimplePanel();
		info.getSimpleTest().test(sp);
		grid.setWidget(i, 2, sp);

		Button button = new Button("sources", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ExternalTextResource res = info.getSource();
				if (res == null)
					return;
				try {
					res.getText(new ResourceCallback<TextResource>() {
						@Override
						public void onSuccess(TextResource resource) {
							String s = resource.getText();
							int i = s.indexOf("/*---begintestmaincode---*/");
							int j = s.indexOf("/*---endtestmaincode---*/");
							try {
								s = s.substring(i + "/*---begintestmaincode---*/".length(), j);
							} catch (Exception e) {
								e.printStackTrace();
							}
							GUIUtil.showText("Source", s);
						}

						@Override
						public void onError(ResourceException e) {
							System.out.println("ERROR getting resource " + info.getSimpleTest().getClass());
						}
					});
				} catch (ResourceException e) {
					e.printStackTrace();
				}
			}
		});
		grid.setWidget(i, 3, button);

		if(info.getEditor()!=null) {
		boolean isSimple = info.getEditor().isSimple();
		BooleanEditor isCOmplexEd = new BooleanEditor();
		isCOmplexEd.load(isSimple);
		grid.setWidget(i, 4, isCOmplexEd.getWidget());
		}
	}

	// test stuff
	@Override
	public String getName() {
		return "editors gallery";
	}

	@Override
	public Map<String, ExternalTextResource> getJavaSources() {
		return TestUtil.getResource(getName(), TestResources.INSTANCE.EditorsGalleryTest());
	}

	@Override
	public String[] getTags() {
		return new String[] {};
	}

	@Override
	public String getDescription() {
		return "this tests show all the supported tests in a list";
	}
}
